package com.shsmi.dsn.controller;

import com.shsmi.dsn.common.Result;
import com.shsmi.dsn.domain.DsnTask;
import com.shsmi.dsn.service.TaskService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Example;
import org.springframework.data.domain.ExampleMatcher;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/")
public class TaskController {

    @Autowired
    private TaskService taskService;

    @GetMapping(path = "/task")
    Result list(@RequestParam(required = false) String inspector,
                @RequestParam(required = false) String current_state,
                @RequestParam(required = false, defaultValue = "desc") String sort_type,
                @RequestParam(required = false, defaultValue = "id") String sort_by,
                @RequestParam(required = false, defaultValue = "1") Integer page_number,
                @RequestParam(required = false, defaultValue = "10") Integer page_size) {
        DsnTask record = new DsnTask();
        if (inspector != null) {
            record.setInspector(inspector);
        }
        if (current_state != null) {
            String[] elements =current_state.split(",");

            record.setInspector(inspector);
        }


        ExampleMatcher matcher = ExampleMatcher.matching()
                .withMatcher("inspector", ExampleMatcher.GenericPropertyMatchers.contains());
        Example<DsnTask> example = Example.of(record,matcher);
        return Result.genSuccessResult(taskService.listAll(example, PageRequest.of(page_number - 1, page_size, Sort.Direction.fromString(sort_type), sort_by)));
    }


    @GetMapping(path = "/task/{id}")
    Result getById(@PathVariable Long id) {
        try {
            DsnTask record = taskService.getById(id);
            return Result.genSuccessResult(record);
        } catch (Exception e) {
            return Result.genFailResult();
        }

    }

    @PostMapping(path = "/task")
    Result create(DsnTask record) {
        try {
            //DsnTask record = new Gson().fromJson(request, DsnTask.class);
            return Result.genSuccessResult(taskService.insert(record));
        } catch (Exception e) {
            return Result.genFailResult();
        }

    }

    @PostMapping(path = "/task/{id}")
    Result update(@PathVariable long id,
                  DsnTask record) {
        try {
            DsnTask tempRecord = taskService.getById(id);
            BeanUtils.copyProperties(record, tempRecord, new String[]{"id"});
            return Result.genSuccessResult(taskService.update(tempRecord));
        } catch (Exception e) {
            return Result.genFailResult();
        }
    }

    @DeleteMapping(path = "/task/{id}")
    Result delete(@PathVariable Long id) {
        try {
            taskService.delete(id);
            return Result.genSuccessResult();
        } catch (Exception e) {
            return Result.genFailResult();
        }
    }
}
